﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;
using Common;
using DAO;
using System.Xml.Linq;

namespace WebComputers.Class
{
    public static class DAHelper
    {
        internal static int CalculateTotalPages(int totalPages, int pageSize)
        {
            return (int)Math.Ceiling((double)totalPages / pageSize);
        }

        internal static bool CheckNumber(string checkValue)
        {
            try
            {
                int check = Convert.ToInt32(checkValue);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        internal static string ImagePath(int st = 0)
        {
            if (st == 0)
                return "../Images/";
            else
                return "Images/";
        }

        internal static string NoImage(int st = 0)
        {
            if (st == 0)
                return "../Images/no_image.gif";
            else
                return "Images/no_image.gif";
        }

        internal static string ImageName(string fileName)
        {
            DateTime date = DateTime.Now;
            return date.Year.ToString() + date.Month + date.Day + date.Hour + date.Minute + date.Second + "_" + fileName;

        }

        internal static string FillImage(string fileName)
        {
            if (!string.IsNullOrEmpty(fileName))
            {
                return DAHelper.ImagePath(1) + fileName;
            }
            else
            {
                return DAHelper.NoImage(1);
            }
        }

        internal static string AddToCart(int id)
        {
            CartManager mng = new CartManager();
            Cart cart = new Cart();
            DanhMucHangHoa dmhh = DanhMucHangHoaDAO.SelectByIdLinq(id);
            cart.dGiaBan = dmhh.dGiaBan;
            cart.iDanhMucHangHoa_ID = dmhh.iDanhMucHangHoa_ID;
            cart.iSoLuong = 1;
            DonViTinh dvt = DonViTinhDAO.SelectById(dmhh.iDonViTinh_ID);
            cart.sDonViTinh = dvt.sTenDonViTinh;
            cart.sHinhDaiDien = dmhh.sHinhDaiDien;
            cart.sMaSanPham = dmhh.sMaHangHoa;
            cart.sTenSanPham = dmhh.sTenHangHoa;
            mng.Add(cart);
            XElement xml = new XElement("Cart", new XElement("TongSoLuong", string.Format("{0:n0}", mng.TotalQuantity())), new XElement("TongTien", string.Format("{0:n0}", mng.TotalCost())));
            return xml.ToString();
        }
        // Từ khóa this ở phần tham số chỉ ra rằng nó lấy chính biến mà đang gọi phương thức này làm tham số
        // Một ví dụ của extension method (extension method phải nằm trong static class)
        internal static DataTable ToDataTable<T>(this IEnumerable<T> items)
        {
            // Create the result table, and gather all properties of a T        
            DataTable table = new DataTable(typeof(T).Name);
            PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            // Add the properties as columns to the datatable
            foreach (var prop in props)
            {
                Type propType = prop.PropertyType;

                // Is it a nullable type? Get the underlying type 
                if (propType.IsGenericType && propType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                    propType = new NullableConverter(propType).UnderlyingType;

                table.Columns.Add(prop.Name, propType);
            }

            // Add the property values per T as rows to the datatable
            foreach (var item in items)
            {
                var values = new object[props.Length];
                for (var i = 0; i < props.Length; i++)
                    values[i] = props[i].GetValue(item, null);

                table.Rows.Add(values);
            }

            return table;
        }

        internal static string ConvertToXml(object obj)
        {
            IEnumerable<DanhMucHangHoa> dmhh = obj as IEnumerable<DanhMucHangHoa>;
            XElement xml = new XElement("SanPham", dmhh.Select(q => new XElement("DanhMucHangHoa",
                //new XAttribute("iDanhMucHangHoa_ID", q.iDanhMucHangHoa_ID),
                new XElement("iDanhMucHangHoa_ID", q.iDanhMucHangHoa_ID),
                new XElement("sTenHangHoa", q.sTenHangHoa),
                new XElement("sHinhDaiDien", string.IsNullOrEmpty(q.sHinhDaiDien)? NoImage(1):ImagePath(1)+q.sHinhDaiDien),
                new XElement("dGiaBan", string.Format("{0:n0}", q.dGiaBan)))));
            return xml.ToString();
        }
    }
}